package com.example.dsplatform.config.AopAspect;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.example.dsplatform.Dao.Mapper.UserMapper;
import com.example.dsplatform.Service.LogsService;
import com.example.dsplatform.Service.UserService;
import com.example.dsplatform.pojo.Entity.LoginUser;
import com.example.dsplatform.pojo.Entity.Logs;
import com.example.dsplatform.pojo.Entity.User;
import com.example.dsplatform.utls.IpUtils;
import com.example.dsplatform.utls.JwtUtil;
import com.example.dsplatform.utls.RedisCache;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.el.parser.Token;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Component
@Slf4j
@Aspect
public class LogsAspect {
     @Autowired
     LogsService logsService;
     @Autowired
     UserService userService;
     @Autowired
     UserMapper userMapper;
     @Autowired
     RedisCache redisCache;


     @AfterReturning(pointcut = "@annotation(honeyLogs)",returning = "jsonResult")
     public void recordLog(JoinPoint jointPoint, HoneyLogs honeyLogs, Object jsonResult) throws Exception {


          ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

          HttpServletRequest request = servletRequestAttributes.getRequest();
          /**
          获取来自前端的请求
           */

          /*   String token = request.getHeader("token");
          String userid;
          try {
               Claims claims = JwtUtil.parseJWT(token);
               userid = claims.getSubject();
          } catch (Exception e) {
               e.printStackTrace();
               throw new RuntimeException("token不合法");
          }*/
          /**
           捕获token
           */
          String token = request.getHeader("token");

          Claims claims = JwtUtil.parseJWT(token);

          String userid = claims.getSubject();

          try {
               claims = JwtUtil.parseJWT(token);
               userid = claims.getSubject();
          } catch (Exception e) {
               e.printStackTrace();
               throw new RuntimeException("token不合法");
          }
          String rediskey = "login:" + userid;
          LoginUser loginUser = redisCache.getCacheObject(rediskey);
          if (Objects.isNull(loginUser)) {
               throw new RuntimeException("用户未登录");
          }


          String ip = IpUtils.getIpAddr(request);
          Logs logs = Logs.builder()
                  .operation(honeyLogs.operation())
                  .type(String.valueOf(honeyLogs.type()))
                  .ip(ip)
                  .users("(id:"+userid+")"+loginUser.getUsername())
                  .time(DateUtil.now())
                  .build();

          ThreadUtil.execAsync(() -> {
               // 异步记录日志信息
               logsService.save(logs);
          });
     }



}
